package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import com.cqvie.model.Course;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

@Repository("courseDao")
public class CourseDao {
	@Resource
	private SessionFactory sessionFactory;

	//查询全部课程
	@SuppressWarnings("unchecked")
	public List<Course> selectAllCourse() {
		String hql = "from Course";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		List<Course> listAll =query.list();
		if (query.list().size() >= 1) {
			return listAll;
		}else
			return null;
	}
	
	//根据课程名称来获取课程
	@SuppressWarnings("unchecked")
	public List<Course> selectByName(String courseName) {
		String hql = "from Course course where course.courseName=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, courseName);
		List<Course> list =query.list();
		if (query.list().size() >= 1) {
			return list;
		}else
			return null;
	}
	
	//根据ID来查询课程
	@SuppressWarnings("unchecked")
	public List<Course> selectByID(int id){
		String hql="from Course course where course.courseID like ?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, id);
		List<Course> courseById =(List<Course>) query.list();
		if(query.list().size()>=1){
			return courseById;
		}
			return null;
	}
	
	//添加课程信息
	public void addCourse(Course c){
		Session session=sessionFactory.getCurrentSession();
		session.save(c);
	}
}
